import common.CommonDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;

/**
 * iframe元素会创建包含另外一个文档的内联框架（即行内框架）
 */
public class IframeAndFrame {

    CommonDemo commonDemo = new CommonDemo();
    private ChromeDriver driver;
    private String url = "src/main/resources/IframeAndFrame.html";
    private WebDriverWait wait;

    @BeforeTest
    public void setUp() throws Exception{
        //获取chromedriver
        driver = commonDemo.getChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        String filepath = commonDemo.getUserDir(url);
        driver.get(filepath);
        driver.manage().window().maximize();
    }

    /**
     * 使用WebElement
     */
    @Test
    public void test_01(){
        //切换到iframe
        WebElement element = driver.findElement(By.cssSelector("#button-frame"));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#button-frame")));
        driver.switchTo().frame(element);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("h1")));
        String content = driver.findElement(By.tagName("h1")).getText();
        System.out.println(content);
        driver.switchTo().defaultContent();
    }

    /**
     * 使用iframe name属性
     */
    @Test
    public void test_02(){
        //切换到iframe
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("my-frame")));
        driver.switchTo().frame("my-frame");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("h1")));
        String content = driver.findElement(By.tagName("h1")).getText();
        System.out.println(content);
        driver.switchTo().defaultContent();
    }

    /**
     * 使用iframe id属性
     */
    @Test
    public void test_03(){
        //切换到iframe
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("button-frame")));
        driver.switchTo().frame("button-frame");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("h1")));
        String content = driver.findElement(By.tagName("h1")).getText();
        System.out.println(content);
        driver.switchTo().defaultContent();
    }

    /**
     * 通过index定位iframe
     */
    @Test
    public void test_04(){
        //切换到iframe
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("button-frame")));
        //通过index定位iframe
        driver.switchTo().frame(0);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("h1")));
        String content = driver.findElement(By.tagName("h1")).getText();
        System.out.println(content);
        driver.switchTo().defaultContent();
    }

    @AfterClass
    public void tearDown() throws Exception{
        Thread.sleep(2000);
        driver.quit();
    }
}
